package com.moonshineproject.plugin.console
{
	import __AS3__.vec.Vector;
	
	import com.moonshineproject.text.TextLineModel;

	public class MarkupTextLineModel extends TextLineModel
	{
		protected var markupText:String;
		
		public function MarkupTextLineModel(text:String)
		{
			markupText = text;
			super( decode(text) );
		}
		
		private function decode(markup:String):String
		{
			var t:String = "";
			var m:Vector.<int> = Vector.<int>([]);
			
			var style2int:Object = ConsoleStyle.name2style;
			
			XML.ignoreWhitespace = false;
			var xml:XML = new XML("<markup>" + markup + "</markup>");
			
			var kids:XMLList = xml.children();
			for each (var node:XML in kids)
			{
				// Add style position
				m[m.length] = t.length;
				
				// Add style value
				if (node.name()  && style2int.hasOwnProperty(node.name()))
				{
					m[m.length] = style2int[node.name().toString().toLowerCase()]; 
				}
				else
				{
					m[m.length] = 0; // Default style
				}
				
				// Build string without markup
				t += node.toString();
			}
			
			meta = m;
			return t;
		}
		
	}
}